# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
import hashlib
from scrapy.pipelines.images import ImagesPipeline
from scrapy.utils.python import to_bytes


class ImagePipeline(ImagesPipeline):

    def file_path(self, request, response=None, info=None, *, item=None):
        image_guid = hashlib.sha256(to_bytes(request.url)).hexdigest()
        return f"{image_guid}.jpg"


class ItblogPipeline:
    def process_item(self, item, spider):
        data = ItemAdapter(item).asdict()
        new = {
            "image_path": data["images"][0]["path"],
            "title": data["title"]
        }

        # print(item)
